package com.ywt.fragment;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.actionbarsherlock.view.SubMenu;
import com.ywt.LoginActivity;
import com.ywt.fragment.home.SampleItem;
import com.ywt.fragment.home.TestData;
import com.ywt.main.R;
import com.ywt.model.ChannelsModel;
import com.ywt.ui.NavigationFragment;
import com.ywt.ui.NavigationFragment.onSelectorNavigationListener;
import com.ywt.utils.Utils;

public class HomeFragment extends SherlockFragment implements
		onSelectorNavigationListener {

//	public static List<ChannelsModel> navs = TestData.buildNavigation();
	public static List<ChannelsModel> navs = Utils.channelModels;
	private NavigationFragment mNavigationFragment;
 
	private TextView tv_title_area;
	private ImageView img_logo;
	
	
	View v;
	private ListView lv;
	SampleAdapter adapter;
	boolean firstLoad = true;
	public static int selected_channel = 0;//选中频道
	
	private Button btn_login;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (v == null) {
			v = inflater.inflate(R.layout.frag_home, container, false);
		 
			lv = (ListView) v.findViewById(R.id.listView1);
			btn_login = (Button)v.findViewById(R.id.btn_login);
			tv_title_area = (TextView)v.findViewById(R.id.tv_title_area);
			img_logo = (ImageView)v.findViewById(R.id.logo_home);
			
			btn_login.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					 LoginActivity.actionLogin(getActivity());		
					  
				}
			});
			mNavigationFragment = (NavigationFragment) getActivity() .getSupportFragmentManager().findFragmentById(
							R.id.fragment_navigation);
			mNavigationFragment.setOnSelectorNavigationListener(this);

		} else {
			ViewGroup p = (ViewGroup) v.getParent();
			if (p != null) {
				p.removeAllViewsInLayout();
			}
		}
		this.setHasOptionsMenu(true);
		return v;
	}

 
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		fillContent(navs.get(selected_channel), selected_channel);

		if (firstLoad)
			firstLoad = false;
		else
			mNavigationFragment.setSelectedTabPosition(selected_channel); 
	}

	
	
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume(); 
		if (Utils.loginModes==null) { 
			btn_login.setVisibility(View.VISIBLE);
			tv_title_area.setVisibility(View.GONE);
			img_logo .setVisibility(View.GONE);
		}else { 
			btn_login.setVisibility(View.GONE);
			tv_title_area.setVisibility(View.VISIBLE);
			img_logo .setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void selector(ChannelsModel navigation, int position) {
		selected_channel = position;
		fillContent(navigation, position);
	}

	private void fillContent(ChannelsModel navigation, int position) {

		SampleAdapter adapter = new SampleAdapter(getActivity());
		adapter.add(TestData.getAdapter(position));
		adapter.add(TestData.getAdapter(position));
		adapter.add(TestData.getAdapter(position));
		adapter.add(TestData.getAdapter(position));
		adapter.add(TestData.getAdapter(position));
		lv.setAdapter(adapter);

		 

		// Fragment fragment = null;
		// switch (navigation.getType()) {
		// case Navigation.TYPE_0:
		// fragment = ContentFragment0.instance(navigation);
		// break;
		// case Navigation.TYPE_1:
		// fragment = ContentFragment1.instance(navigation);
		// break;
		// case Navigation.TYPE_2:
		// fragment = ContentFragment2.instance(navigation);
		// break;
		// case Navigation.TYPE_3:
		// fragment = ContentFragment3.instance(navigation);
		// break;
		// case Navigation.TYPE_4:
		// fragment = ContentFragment4.instance(navigation);
		// break;
		// case Navigation.TYPE_5:
		// fragment = ContentFragment5.instance(navigation);
		// break;
		// }
		// FragmentManager fragmentManager =
		// getActivity().getSupportFragmentManager();
		// FragmentTransaction transaction = fragmentManager.beginTransaction();
		// transaction.replace(R.id.fl_content, fragment);
		// transaction.commit();
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		MenuItem ads = menu.add("homeads");

		ads.setIcon(R.drawable.menu_sync);
		ads.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		ads.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Toast.makeText(getActivity(), "ͬ同步更新", Toast.LENGTH_LONG)
						.show();
				return true;
			}
		});

		SubMenu sub = menu.addSubMenu("Theme");
		sub.setIcon(R.drawable.menu_add_note);
		sub.add(0, R.style.Theme_Sherlock, 0, "Default");
		sub.add(0, R.style.Theme_Sherlock_Light, 0, "Light");
		sub.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0,
				"Light (Dark Action Bar)");
		sub.getItem().setShowAsAction(
				MenuItem.SHOW_AS_ACTION_ALWAYS
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);

		// SubMenu subMenu1 = menu.addSubMenu("Action Item");
		// subMenu1.add("Sample");
		// subMenu1.add("Menu");
		// subMenu1.add("Items");
		//
		// MenuItem subMenu1Item = subMenu1.getItem();
		// subMenu1Item.setIcon(R.drawable.menu_add_note);
		// subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
		// MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

//		if (item.getItemId() == android.R.id.home || item.getItemId() == 0) {
//			return false;
//		}
//
//		Toast.makeText(getActivity(),
//				"Theme changed to \"" + item.getTitle() + "\"",
//				Toast.LENGTH_SHORT).show();
		return true;
	}

	public class SampleAdapter extends ArrayAdapter<SampleItem> {

		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.frag_home_list_item2, null);
			} 
			TextView title = (TextView) convertView .findViewById(R.id.row_title);
			TextView count = (TextView) convertView .findViewById(R.id.row_count);

//			TextView unit = (TextView) convertView.findViewById(R.id.row_unit);
			TextView price = (TextView) convertView.findViewById(R.id.row_price);

			TextView type = (TextView) convertView.findViewById(R.id.row_type);
			TextView city = (TextView) convertView.findViewById(R.id.row_city);
			TextView date = (TextView) convertView.findViewById(R.id.row_date);

			title.setText(getItem(position).title);
			count.setText(getItem(position).count);
//			unit.setText(getItem(position).unit);
			price.setText(getItem(position).price);
			type.setText(getItem(position).type);
			city.setText(getItem(position).city);
			date.setText(getItem(position).date);
			return convertView;
		}
	}
}